import { PRIORITY_VIEW } from "../../../Core/CoreDefine";
import { GCtrl } from "../../../Core/GCtrl";
import GChild from "../../../Core/GView/GChild";
import { CMsg, LinearState, PART_BTL_SCENE } from "../../Common/Define";
import { L } from "../../Common/Language";
import { VIEW_ID } from "../../Common/UI";
import { Res } from "../../Common/UIResources";
import { JXLocales } from "../../Common/Zh";
import RJXItem from "../../Data/RunTime/RunRaw/RJXItem";
import GameMgr from "../../Logic/GameMgr";
import HeroChange from "../Hero/HeroChange";
import { JXEAniNames } from "../JXBattle/JXULDefine";

const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Home/TowerFloorItem')

export default class TowerFloorItem extends GChild {
    @property(cc.Sprite) img: cc.Sprite = null;
    /** 未解锁标识 */
    @property(cc.Node) unLockNode: cc.Node = null;
    @property({ type: cc.Label, tooltip: "奖励收益" }) reward: cc.Label = null;
    @property({ type: cc.Label, tooltip: "奖励收益1" }) reward1: cc.Label = null;
    @property({ type: cc.Label, tooltip: "场" }) ground: cc.Label = null;
    @property(HeroChange) monster: HeroChange = null;
    @property(HeroChange) hero: HeroChange = null;
    protected _sportsData: STowerDataRaw = null;

    public onGLoad(): void {
        GCtrl.ES.on(CMsg.client.fight_ui.onFinishSettle, this, this.onCMSGBattleResult.bind(this), PRIORITY_VIEW);
        GCtrl.ES.on(CMsg.client.tower.onStartFight, this, this.onStartFight.bind(this), PRIORITY_VIEW);
        GCtrl.ES.on(CMsg.client.fight_ui.onRetreatClick, this, this.onRetreatClick.bind(this), PRIORITY_VIEW)
    }

    public setView(lv: number) {
        this._sportsData = GameMgr.towerData.getRaw<STowerDataRaw>(lv)
        this.ground.string = L(JXLocales.tower.level, lv);
        this.setItem();
        let lastPassId = GameMgr.lUserData.getLastTowerId();
        if (lastPassId >= lv) {
            this.setLinearState(LinearState.Pass);
            this.setMonster(false);
        } else {
            this.setMonster(true);
            if (lastPassId + 1 == lv) {
                this.setLinearState(LinearState.Now);
            } else {
                this.setLinearState(LinearState.Future);
            }
        }
    }

    /**设置状态 */
    protected setLinearState(state: LinearState) {
        if (state === LinearState.Now) {
            this.hero.node.opacity = 0;
            this.hero.init(1001)
            this.hero.node.active = true;
            cc.tween(this.hero.node).to(1.5, { opacity: 255 }).start();
        } else {
            this.hero.node.active = false;
        }
        this.unLockNode.active = state == LinearState.Future;
        let imgName = state === LinearState.Now ? "tower2" : "tower1";
        this.assetImpl.spriteAtlasFrame(this.img, Res.texture.views.tower, imgName)
    }

    /**设置英雄*/
    protected setMonster(isActive: boolean) {
        this.monster.node.active = isActive;
        if (isActive) {
            let hero = GameMgr.rHeroData.createHeroData(this._sportsData.guard) as any;
            this.monster.init(hero)
        }
    }

    /**设置 */
    protected setItem() {
        let rjxItem1 = new RJXItem(this._sportsData.hook[0]);
        let rjxItem2 = new RJXItem(this._sportsData.hook[1]);
        this.reward.string = rjxItem1.num + "/分";
        this.reward1.string = rjxItem2.num + "/分";
    }

    public onShow(cb) {
        this.unLockNode.active = true;
        let right = this.unLockNode.children[0];
        let left = this.unLockNode.children[1];
        cc.tween(right).to(0.5, { x: this.node.width / 2, opacity: 0 }).start();
        cc.tween(left).to(0.5, { x: -this.node.width / 2, opacity: 0 }).call(() => {
            this.setLinearState(LinearState.Now)
        }).start();
        cb();
    }

    //#region 表现
    public onPass(cb) {
        let onDieAnimationEnd = false;
        let node = this.monster.heroNode;
        let skeleton = node.getComponent(sp.Skeleton);
        skeleton.premultipliedAlpha = false;
        let heroSkele = this.hero.heroNode.getComponent(sp.Skeleton)
        heroSkele.setAnimation(0, JXEAniNames.skill + 1, false);
        if (!onDieAnimationEnd) {
            heroSkele.setCompleteListener(() => {
                heroSkele.setCompleteListener(null)
                skeleton.setAnimation(0, "die", false);
                if (!onDieAnimationEnd) {
                    onDieAnimationEnd = true;
                    skeleton.setCompleteListener(() => {
                        skeleton.setCompleteListener(null);
                        this.onDieAnimationEnd(cb);
                    })
                }
            })
        }
    }

    /** 死亡动画播放结束 */
    protected onDieAnimationEnd(cb) {
        cc.tween(this.monster.node).to(0.5, { opacity: 0 }).start();
        cc.tween(this.hero.node).delay(0.5).to(1, { opacity: 0 }).call(() => {
            this.setLinearState(LinearState.Pass)
            this.hero.node.active = false;
            this.hero.node.opacity = 255;
            cb();
        }).start();
    }


    protected onStartFight(_, id: number): void {
        if (id != this._sportsData.id) return;
        let onAtkAnimationEnd = false;
        let heroSkele = this.hero.heroNode.getComponent(sp.Skeleton)
        heroSkele.premultipliedAlpha = false;
        heroSkele.setAnimation(0, JXEAniNames.run, true);
        cc.tween(this.hero.node).to(0.5, { position: cc.v2(-25, this.hero.node.position.y) }).call(() => {
            heroSkele.setAnimation(0, JXEAniNames.skill + 1, false)
            if (!onAtkAnimationEnd) {
                onAtkAnimationEnd = true;
                heroSkele.setCompleteListener(() => {
                    heroSkele.setCompleteListener(null);
                    heroSkele.setAnimation(0, JXEAniNames.idle, false);
                    let node = this.monster.heroNode;
                    let skeleton = node.getComponent(sp.Skeleton);
                    skeleton.setAnimation(0, JXEAniNames.hit, false);
                    this.scheduleOnce(() => {
                        skeleton.setAnimation(0, JXEAniNames.idle, true);
                        let args: ArgsBattleViewCtrl<SceneGuanQia> = {
                            sceneId: PART_BTL_SCENE.ARENA,
                            args: { level: id }
                        }
                        GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.fightLayer, args)
                    }, 0.5)
                })
            }

        }).start()
    }

    protected onRetreatClick(): void {
        if (this.hero.node.active && this.hero.node.x != -115) {
            let heroSkele = this.hero.heroNode.getComponent(sp.Skeleton)
            this.hero.node.scaleX *= -1;
            heroSkele.setAnimation(0, JXEAniNames.run, true);
            cc.tween(this.hero.node).to(0.5, { position: cc.v2(-115, this.hero.node.position.y) }).call(() => {
                heroSkele.setAnimation(0, JXEAniNames.idle, true);
                this.hero.node.scaleX *= -1;
            }).start();
        }
    }


    protected onCMSGBattleResult(_, result: { sceneId: number, isWin: boolean }) {
        if (result.sceneId != PART_BTL_SCENE.ARENA) return;
        let isWin = result.isWin;
        if (isWin) {
            return;
        }
        this.onRetreatClick();
    }
}
